package com.annoncesautomobile.vue.rechercheAnnonce;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.annoncesautomobile.R;
import com.annoncesautomobile.modele.util.IModelXML;
import com.annoncesautomobile.modele.util.Requete;
import com.annoncesautomobile.modele.util.ModelXML;
import com.annoncesautomobile.vue.SuperActivity;

public class ChoixNbPorteActivity extends SuperActivity {

	private IModelXML modele;
	private ListView lv;
	private ArrayAdapter<String> adapter;
	private Requete request;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_choix_categorie);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
		TextView textViewActivityTitleText = (TextView) findViewById(R.id.activity_title_text);
		textViewActivityTitleText.setText(getString(R.string.title_activity_choix_nb_porte));

		addBoutonBas();
		
		lv = (ListView) this.findViewById(R.id.listViewChoixItem);
		this.modele = ModelXML.getInstance();
		this.request = (Requete) getIntent().getParcelableExtra("requete");
		
		ArrayList<String> listannee = (ArrayList<String>) modele.getNbPorte();
		if(this.getIntent().getBooleanExtra("AllAccepted", true))
		{
			listannee.add(0,"Toutes portes");
		}
		adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, listannee);
		lv.setAdapter(adapter);

		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {

				String nbPorte =  (String) parent.getItemAtPosition(position);
				if(nbPorte.equals("Toutes portes"))
				{
					request.remove("nb_portes");
				}
				else
				{
					request.put("nb_portes", nbPorte, nbPorte + " portes");
				}
				Intent output = new Intent();
				output.putExtra("requete", (Parcelable) request);
				setResult(RESULT_OK, output);
				finish();
			}
		});

	}
	
	public void onBackPressed(View view) {
		this.onBackPressed();
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
}
